package br.com.felix.dbm.util.io;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import br.com.felix.dbm.database.DataModel;

public class FDMFileManager {

	
	public void save(DataModel dm, File targetFile) throws IOException{
		if(!targetFile.exists()){
			targetFile.createNewFile();
		}
		FileOutputStream fos = new FileOutputStream(targetFile);
		ObjectOutputStream oos = new ObjectOutputStream(fos);
		oos.writeObject(dm);
		oos.close();
	}

	
	public DataModel open(File file) throws IOException {
		if(!file.exists())return null;
		FileInputStream fis = new FileInputStream(file);
		ObjectInputStream ois = new ObjectInputStream(fis);
		try {
			DataModel dm = (DataModel)ois.readObject();
			ois.close();
			return dm;
		} 
		catch (ClassNotFoundException e) {
			throw new IOException("Error reading object.",e);
		}
	}
}
